package com.ruoyi.datastatistics.service.impl;

import com.ruoyi.common.core.domain.Parameter;
import com.ruoyi.common.core.feign.ModulesSystemFeign;
import com.ruoyi.datastatistics.domain.AgegroupResult;
import com.ruoyi.datastatistics.domain.LevelResult;
import com.ruoyi.datastatistics.domain.LevelgroupResult;
import com.ruoyi.datastatistics.domain.PassrateResult;
import com.ruoyi.datastatistics.mapper.PassrateMapper;
import com.ruoyi.datastatistics.mapper.RatingLevelMapper;
import com.ruoyi.datastatistics.service.AgegroupResultSorter;
import com.ruoyi.datastatistics.service.LevelgroupResultSorter;
import com.ruoyi.datastatistics.service.RatingLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class RatingLevelServiceImpl implements RatingLevelService {
    @Autowired
    public ModulesSystemFeign systemFeign;
    @Autowired
    private RatingLevelMapper ratingLevelMapper;
    @Override
    public Map<String, Object> getTotallevel(Parameter parameter) {
        Map<String,Object> dataMap = new HashMap<>();
        parameter = systemFeign.setParameter(parameter);
        if (parameter.getOrganizationCode() != null && !parameter.getOrganizationCode().equals("")){
            List<LevelResult> LevelResultlist = ratingLevelMapper.getTotallevel(parameter);
            if (LevelResultlist.size() > 0){
                List<LevelgroupResult> Result = setLevelgroupResultList(LevelResultlist);
                dataMap.put("data",Result);
                dataMap.put("message",true);
                dataMap.put("parameter",parameter);
            }else {
                dataMap.put("data",null);
                dataMap.put("message","请在机构管理中添加该机构信息");
            }
        }else {
            dataMap.put("data",null);
            dataMap.put("message","请在机构管理中添加该机构信息");
        }
        return dataMap;
    }

    @Override
    public Map<String, Object> getAgegrouplevel(Parameter parameter) {
        parameter = systemFeign.setParameter(parameter);
        Map<String,Object> dataMap = new HashMap<>();
        if (parameter.getOrganizationCode() != null && !parameter.getOrganizationCode().equals("")){
            List<LevelResult> LevelResultlist = ratingLevelMapper.getAgegrouplevel(parameter);
            if (LevelResultlist.size() > 0){
                List<LevelgroupResult> Result = setLevelgroupResultList(LevelResultlist);
                // 定义排序顺序
                List<String> customOrder  = Arrays.asList("幼儿", "成年","老年");
                // 按照自定义顺序排序
                List<LevelgroupResult> sortedResults = LevelgroupResultSorter.sortByCustomOrder(
                        Result,
                        LevelgroupResult::getName,
                        customOrder
                );
                dataMap.put("data",sortedResults);
                dataMap.put("message",true);
            }
        }else {
            dataMap.put("data",null);
            dataMap.put("message","请在机构管理中添加该机构信息");
        }
        return dataMap;
    }

    @Override
    public Map<String, Object> getAgebracketlevel(Parameter parameter) {
        parameter = systemFeign.setParameter(parameter);
        Map<String,Object> dataMap = new HashMap<>();
        if (parameter.getOrganizationCode() != null && !parameter.getOrganizationCode().equals("")){
            List<LevelResult> LevelResultlist = ratingLevelMapper.getAgebracketlevel(parameter);
            if (LevelResultlist.size() > 0){
                List<LevelgroupResult> Result = setLevelgroupResultList(LevelResultlist);
                // 定义排序顺序
                List<String> customOrder  = Arrays.asList("3岁", "3.5岁","4岁", "4.5岁","5岁", "5.5岁","6岁","6.5岁", "20岁~24岁","25岁~29岁", "30岁~34岁","35岁~39岁", "40岁~44岁","45岁~49岁", "50岁~54岁","55岁~59岁", "60岁~64岁","65岁~69岁", "70岁~74岁","75岁~79岁");
                // 按照自定义顺序排序
                List<LevelgroupResult> sortedResults = LevelgroupResultSorter.sortByCustomOrder(
                        Result,
                        LevelgroupResult::getName,
                        customOrder
                );
                dataMap.put("data",sortedResults);
                dataMap.put("message",true);
            }
        }else {
            dataMap.put("data",null);
            dataMap.put("message","请在机构管理中添加该机构信息");
        }
        return dataMap;
    }

    @Override
    public Map<String, Object> getUrbanandrurallevel(Parameter parameter) {
        parameter = systemFeign.setParameter(parameter);
        Map<String,Object> dataMap = new HashMap<>();
        if (parameter.getOrganizationCode() != null && !parameter.getOrganizationCode().equals("")){
            List<LevelResult> LevelResultlist = ratingLevelMapper.getUrbanandrurallevel(parameter);
            if (LevelResultlist.size() > 0){
                List<LevelgroupResult> Result = setLevelgroupResultList(LevelResultlist);
                // 定义排序顺序
                List<String> customOrder  = Arrays.asList("城镇幼儿", "农村幼儿", "城镇非体力劳动者", "城镇体力劳动者", "农民", "城镇老年人", "农村老年人");
                // 按照自定义顺序排序
                List<LevelgroupResult> sortedResults = LevelgroupResultSorter.sortByCustomOrder(
                        Result,
                        LevelgroupResult::getName,
                        customOrder
                );
                dataMap.put("data",sortedResults);
                dataMap.put("message",true);
            }
        }else {
            dataMap.put("data",null);
            dataMap.put("message","请在机构管理中添加该机构信息");
        }
        return dataMap;
    }

    @Override
    public Map<String, Object> getMonthlevel(Parameter parameter) {
        parameter = systemFeign.setParameter(parameter);
        Map<String,Object> dataMap = new HashMap<>();
        if (parameter.getOrganizationCode() != null && !parameter.getOrganizationCode().equals("")){
            List<LevelResult> LevelResultlist = ratingLevelMapper.getMonthlevel(parameter);
            if (LevelResultlist.size() > 0){
                List<LevelgroupResult> Result = setLevelgroupResultList(LevelResultlist);
                List<LevelgroupResult> sortedList = Result.stream()
                        .sorted((a1, a2) -> {
                            try {
                                int num1 = Integer.parseInt(a1.getName());
                                int num2 = Integer.parseInt(a2.getName());
                                return Integer.compare(num1, num2);
                            } catch (NumberFormatException e) {
                                // 处理非数字情况，例如按字符串排序或赋予默认值
                                return a1.getName().compareTo(a2.getName());
                            }
                        })
                        .collect(Collectors.toList());
                dataMap.put("data",sortedList);
                dataMap.put("message",true);
            } else {
                dataMap.put("data",null);
                dataMap.put("message","请在机构管理中添加该机构信息");
            }
        }
        return dataMap;
    }

    @Override
    public Map<String, Object> getNextdeptlevel(Parameter parameter) {
        parameter = systemFeign.setParameter(parameter);
        Map<String,Object> dataMap = new HashMap<>();
        if (parameter.getOrganizationCode() != null && !parameter.getOrganizationCode().equals("")){
            List<LevelResult> LevelResultlist = ratingLevelMapper.getNextdeptlevel(parameter);
            if (LevelResultlist.size() > 0){
                List<LevelgroupResult> Result = setLevelgroupResultList(LevelResultlist);
                dataMap.put("data",Result);
                dataMap.put("message",true);
            }else {
                dataMap.put("data",null);
                dataMap.put("message","请在机构管理中添加该机构信息");
            }
        }else {
            dataMap.put("data",null);
            dataMap.put("message","请在机构管理中添加该机构信息");

        }
        return dataMap;
    }

    public static double roundToTwoDecimalPlaces(double value) {
        return new BigDecimal(Double.toString(value))
                .multiply(new BigDecimal("100"))  // 先乘以100
                .setScale(1, RoundingMode.HALF_UP)  // 然后保留2位小数
                .doubleValue();
    }
    public List<LevelgroupResult> setLevelgroupResultList(List<LevelResult> LevelResultlist){
        List<LevelgroupResult> levelgroupResultList = new ArrayList<>();
        for (LevelResult levelResult : LevelResultlist){
            LevelgroupResult result = new LevelgroupResult();
            result.setName(levelResult.getName());
            if (levelResult.getAllman() > 0){
                result.setYxman(roundToTwoDecimalPlaces(levelResult.getYxman()/levelResult.getAllman()));
                result.setLhman(roundToTwoDecimalPlaces(levelResult.getLhman()/levelResult.getAllman()));
                result.setYlman(roundToTwoDecimalPlaces((levelResult.getYxman()+levelResult.getLhman())/levelResult.getAllman()));
            }else {
                result.setYxman(0);
                result.setLhman(0);
                result.setYlman(0);
            }
            if (levelResult.getAllwoman() > 0){
                result.setYxwoman(roundToTwoDecimalPlaces(levelResult.getYxwoman()/levelResult.getAllwoman()));
                result.setLhwoman(roundToTwoDecimalPlaces(levelResult.getLhwoman()/levelResult.getAllwoman()));
                result.setYlwoman(roundToTwoDecimalPlaces((levelResult.getYxwoman()+levelResult.getLhwoman())/levelResult.getAllwoman()));
            }else{
                result.setYxwoman(0);
                result.setLhwoman(0);
                result.setYlwoman(0);
            }
            if (levelResult.getAlltotal() > 0){
                result.setYxtotal(roundToTwoDecimalPlaces(levelResult.getYxtotal()/levelResult.getAlltotal()));
                result.setLhtotal(roundToTwoDecimalPlaces(levelResult.getLhtotal()/levelResult.getAlltotal()));
                result.setYltotal(roundToTwoDecimalPlaces((levelResult.getYxtotal()+levelResult.getLhtotal())/levelResult.getAlltotal()));
            }else {
                result.setYxtotal(0);
                result.setLhtotal(0);
                result.setYltotal(0);
            }
            levelgroupResultList.add(result);
        }
        return levelgroupResultList;
    }
}
